<?php

/**
 * Created by PhpStorm.
 * User: li
 * Date: 15-10-18
 * Time: 下午10:57
 */
class Login extends MY_View_Controller
{
    function __construct(){
        parent::__construct();
    }

    function index(){
        $this->display();
    }

    /**
     * 登录接口
     */
    public function do_login()
    {
        $this->load->helper('validate');
        $account = $this->input->post('account');
        $password = $this->input->post('password');
        $device_id = $this->input->post('device_id');
        $device = $this->input->post('device');
        $check_mobile = is_mobile($account);
        $remember = $this->input->post('remember')?$this->input->post('remember'):false;
        $logined=false;
        if (!$check_mobile['status']) {
            $check_account = is_email($account);
            if (!$check_account['status']) {
                $this->json_response(false,'','帐号错误');
            }else{
                $logined=$this->auth_nuan->login($account,$password,true);
            }
        }else{
            $logined=$this->auth_nuan->login($account,$password,false);
        }
        if ($logined){
            if($remember){
                $this->auth_nuan->create_autologin($_SESSION['login_user']['user_id'],
                    $device?array(
                        'device'    =>  $device,
                        'device_id' =>  $device_id)
                        :NULL
                    );
            }
            $username = $this->auth_nuan->get_username();
            $this->json_response(true, array('url'=>@$_SESSION['service']),$username);
        } else {
            $errors = $this->auth_nuan->get_error_message();
            if (isset($errors['banned'])) {								// banned user
                $this->json_response(false, array(), '您被禁止登录');
            } elseif (isset($errors['not_activated'])) {				// not activated user
                $this->json_response(false, array(), '请先激活您的账号');
            } else {
                $this->json_response(false, '', $errors);
            }
        }
    }

    /**
     * 获取用户登录信息
     */
    function get_login(){
        if($this->auth_nuan->is_logged_in())
            $this->json_response(true);
        else
            $this->json_response(false);
    }
}